<template>
  <div class="sso-home-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="logo-container">
        <span class="title">统一认证中心</span>
      </div>

      <!-- 用户信息区域 -->
      <div class="user-info-container">
        <div class="user-info" v-if="username">
          <div class="user-avatar">
            <img src="/avatar.jpg" alt="用户头像" class="avatar" />
          </div>
          <div class="user-details">
            <span class="username">{{ username }}</span>
          </div>

          <!-- 登出悬浮按钮 -->
          <div class="logout-button-container">
            <button class="logout-button" @click="logoutHandler">
              <i class="fa fa-sign-out"></i>
              <span class="logout-text">退出登录</span>
            </button>
          </div>
        </div>
      </div>
    </header>

    <!-- 主内容区域 -->
    <main class="main-content">
      <!-- 欢迎卡片 -->
      <div class="welcome-card">
        <h1 class="welcome-title">欢迎使用统一认证中心</h1>
        <p class="welcome-message">安全、便捷的单点登录服务</p>
      </div>

      <!-- 应用卡片区域 -->
      <div class="app-cards-container">
        <div class="app-card" v-for="app in availableApps" :key="app.id">
          <div class="app-icon">
            <i :class="app.icon"></i>
          </div>
          <h3 class="app-name">{{ app.name }}</h3>
          <p class="app-desc">{{ app.description }}</p>
          <button class="app-launch-btn">
            进入应用
            <i class="fa fa-arrow-right"></i>
          </button>
        </div>
      </div>

      <!-- 最近访问应用 -->
      <div class="recent-apps-container">
        <h2 class="section-title">最近访问</h2>
        <div class="recent-apps-grid">
          <div class="recent-app-card" v-for="app in recentApps" :key="app.id">
            <div class="app-icon-small">
              <i :class="app.icon"></i>
            </div>
            <span class="app-name-small">{{ app.name }}</span>
          </div>
        </div>
      </div>
    </main>

    <!-- 页脚 -->
    <footer class="footer">
      <div class="footer-content">
        <div class="footer-section">
          <h3 class="footer-title">关于我们</h3>
          <p class="footer-text">统一认证中心提供安全便捷的单点登录服务</p>
        </div>
        <div class="footer-section">
          <h3 class="footer-title">联系我们</h3>
          <p class="footer-text">技术支持: support@example.com</p>
        </div>
        <div class="footer-section">
          <h3 class="footer-title">帮助中心</h3>
          <p class="footer-text">查看帮助文档</p>
        </div>
      </div>
      <div class="footer-bottom">
        <p class="copyright">© 2025 统一认证中心 版权所有</p>
      </div>
    </footer>
    <Loading :visible="!username"></Loading>
  </div>
</template>

<script>
import Loading from './Loading.vue'

export default {
  components: {
    Loading,
  },
  data() {
    return {
      username: '',
      availableApps: [
        {
          id: 1,
          name: '企业门户',
          description: '企业综合信息门户',
          icon: 'fa fa-building',
        },
        {
          id: 2,
          name: '邮件系统',
          description: '企业电子邮件服务',
          icon: 'fa fa-envelope',
        },
        {
          id: 3,
          name: '办公系统',
          description: '协同办公平台',
          icon: 'fa fa-file-text',
        },
        {
          id: 4,
          name: '知识库',
          description: '企业知识管理系统',
          icon: 'fa fa-book',
        },
        {
          id: 5,
          name: '项目管理',
          description: '项目进度跟踪系统',
          icon: 'fa fa-tasks',
        },
        {
          id: 6,
          name: '人力资源',
          description: '人力资源管理平台',
          icon: 'fa fa-users',
        },
      ],
      recentApps: [
        { id: 1, name: '企业门户', icon: 'fa fa-building' },
        { id: 2, name: '邮件系统', icon: 'fa fa-envelope' },
        { id: 3, name: '办公系统', icon: 'fa fa-file-text' },
      ],
    }
  },
  mounted() {
    // 发送跨域消息
    if (localStorage.getItem('token')) {
      const token = localStorage.getItem('token')
      window.parent.postMessage(
        { token, type: 'sso-token-login' },
        'http://localhost:8081'
      )
    } else {
      window.parent.postMessage({ type: 'sso-token-nologin' }, '*')
    }
    // 获取用户信息
    this.axios.get('/user-info').then((res) => {
      this.username = res.data.username
    })
  },
  
  methods: {
    logoutHandler() {
      this.axios.get('/logout').then(() => {


        this.$message.success('登出成功');
        localStorage.removeItem('token')
        this.username = ''
        this.$router.push('/login');
      })
    },
  },
}
</script>

<style scoped>
/* 全局样式 */
.sso-home-container {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  margin: 0;
  padding: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8f9fa;
}

/* 头部样式 */
.header {
  background: linear-gradient(135deg, #4a6fe3 0%, #7928ca 100%);
  color: white;
  padding: 15px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  height: 40px;
  margin-right: 15px;
}

.title {
  font-size: 24px;
  font-weight: 500;
}

.user-info-container {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar {
  margin-right: 15px;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2px solid white;
  object-fit: cover;
}

.user-details {
  text-align: right;
  margin-right: 15px;
}

.username {
  font-size: 16px;
  font-weight: 500;
}

.user-role {
  font-size: 14px;
  opacity: 0.8;
}

.logout-button-container {
  position: relative;
}

.logout-button {
  background-color: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  padding: 8px 15px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  transition: background-color 0.3s;
  cursor: pointer;
}

.logout-button:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.logout-text {
  margin-left: 8px;
}

/* 主内容样式 */
.main-content {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
}

.welcome-card {
  background: linear-gradient(135deg, #6c63ff 0%, #8e2de2 100%);
  color: white;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
  text-align: center;
}

.welcome-title {
  font-size: 32px;
  margin-bottom: 10px;
}

.welcome-message {
  font-size: 18px;
  opacity: 0.9;
}

.app-cards-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.app-card {
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  padding: 20px;
  text-align: center;
  transition: transform 0.3s, box-shadow 0.3s;
}

.app-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

.app-icon {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #6c63ff 0%, #8e2de2 100%);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 15px;
  font-size: 24px;
}

.app-name {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 10px;
}

.app-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.app-launch-btn {
  background: linear-gradient(135deg, #6c63ff 0%, #8e2de2 100%);
  color: white;
  border: none;
  padding: 10px 15px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.app-launch-btn:hover {
  background: linear-gradient(135deg, #5a52e0 0%, #7a1dd1 100%);
}

.app-launch-btn i {
  margin-left: 8px;
}

.recent-apps-container {
  margin-bottom: 30px;
}

.section-title {
  font-size: 20px;
  font-weight: 500;
  margin-bottom: 15px;
}

.recent-apps-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}

.recent-app-card {
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.1);
  padding: 10px 15px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.3s;
}

.recent-app-card:hover {
  background-color: #f8f9fa;
}

.app-icon-small {
  width: 30px;
  height: 30px;
  background: linear-gradient(135deg, #6c63ff 0%, #8e2de2 100%);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  font-size: 14px;
}

.app-name-small {
  font-size: 14px;
}

/* 页脚样式 */
.footer {
  background-color: #333;
  color: white;
  padding: 30px;
}

.footer-content {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.footer-section {
  flex: 1;
  margin-right: 30px;
}

.footer-title {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 10px;
}

.footer-text {
  font-size: 14px;
  opacity: 0.8;
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.copyright {
  font-size: 14px;
  opacity: 0.7;
}
</style>